<?php

namespace App\Services\Tbk;

use NiuGengYun\EasyTBK\Factory;
use NiuGengYun\EasyTBK\pinduoduo\request\DdkGoodsSearchRequest;
use NiuGengYun\EasyTBK\pinduoduo\request\DdkMemberAuthorityRequest;
use NiuGengYun\EasyTBK\pinduoduo\request\DdkOauthGoodsRecommendList;
use NiuGengYun\EasyTBK\pinduoduo\request\DdkRpPromUrlGenerateRequest;
use NiuGengYun\EasyTBK\pinduoduo\request\DdkGoodsPromotionUrlGenerateRequest;

class PddService
{
    /**
     * 检查是否授权
     */
    public function searchAuth(string $pid)
    {
        $pdd = Factory::pinduoduo();
        $req = new DdkMemberAuthorityRequest();
        $req->setPid($pid);
        $res = $pdd->execute($req);
        return $res;
    }

    /**
     * 授权
     */
    public function setAuth(string $pid)
    {
        $custom_data = json_encode(['uid' => "1"]);
        $pdd = Factory::pinduoduo();
        $req = new DdkRpPromUrlGenerateRequest();
        $req->setChannelType(10);
        $req->setCustomParameters($custom_data);
        $pidList = "['" . $pid . "']";
        $req->setPid($pidList);
        $res = $pdd->execute($req);
        return $res;
    }

    /**
     * 根据商品链接查询商品信息
     */
    public function searchGoodsInfoByKeyword($keyword, $pid, $customParamJson = "")
    {
        $pdd = Factory::pinduoduo();
        $req = new DdkGoodsSearchRequest();
        $req->setKeyWord($keyword);
        $req->setPid($pid);
        // 8-优惠券金额排序降序
        $req->setSortType(8);
        $req->setCustomParameters($customParamJson);
        try {
            $res = $pdd->execute($req);
        } catch (\Exception $e) {
            dd($e->getMessage());
        }
        return $res;
    }

    /**
     * 根据商品goods_sign获取商品信息
     * @param $sign
     * @param $pid
     * @return mixed
     */
    public function searchGoodsInfoByGoodsSign($sign, $pid)
    {
        $pdd = Factory::pinduoduo();
        $req = new DdkGoodsSearchRequest();
        $req->setPid($pid);
        $goodsSignList = "['" . $sign . "']";
        $req->setGoodsIdList($goodsSignList);
        // 8-优惠券金额排序降序
        $req->setSortType(8);
        try {
            $res = $pdd->execute($req);
        } catch (\Exception $e) {
            dd($e->getMessage());
        }
        return $res;
    }

    /**
     * 转链
     * @param $goodsSign
     * @param $pid
     * @return mixed
     */
    public function updateLink($goodsSign, $pid, $customParamJson = "")
    {
        $pdd = Factory::pinduoduo();
        $req = new DdkGoodsPromotionUrlGenerateRequest();
        $req->setPid($pid);
        $goodsSignList = "['" . $goodsSign . "']";
        $req->setGoodsIdList($goodsSignList);
        $req->setGenerateWeApp("true");
        $req->setCustomParameters($customParamJson);
        try {
            $res = $pdd->execute($req);
        } catch (\Exception $e) {
            dd($e->getMessage());
        }
        return $res;
    }

    /**
     * 获取商品列表
     * @return mixed
     */
    public function getProductList($pid, $customParamJson, string $activityTages, int $page = 1, int $pageSize = 10)
    {
        $pdd = Factory::pinduoduo();
        $req = new DdkGoodsSearchRequest();
        $req->setPid($pid);
        $activityTagesList = "[" . $activityTages . "]";
        $req->setActivityTags($activityTagesList);
        // 2-按佣金比例降序
        $req->setSortType(2);
        $req->setCustomParameters($customParamJson);
        $req->setPage($page);
        $req->setPageSize($pageSize);
        try {
            $res = $pdd->execute($req);
        } catch (\Exception $e) {
            dd($e->getMessage());
        }
        return $res;
    }
}
